package ThreadService;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class NIOClientService {

    private Selector selector;
    private SocketChannel socketChannel;

    public NIOClientService(int port) {
        try {
            selector = Selector.open();
            socketChannel = SocketChannel.open();
            // 连接服务器
            socketChannel.connect(new InetSocketAddress("127.0.0.1", port));
            // 设置非阻塞工作
            socketChannel.configureBlocking(false);
            // 注册我们的通道
            socketChannel.register(selector, SelectionKey.OP_CONNECT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //发送消息
    public void sendInfo(String info) {
        try {
            ByteBuffer wrap = ByteBuffer.wrap(info.getBytes());
            socketChannel.write(wrap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 读取信息
    public void readInfo() {
        try {
            int readChannel = selector.select(2000);
            if (readChannel > 0) {
                // 有可以用的通道
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = selectionKeys.iterator();
                while (iterator.hasNext()) {
                    SelectionKey next = iterator.next();
                    if (next.isReadable()) {
                        SelectableChannel keyChannel = next.channel();
                        if (keyChannel instanceof SocketChannel) {
                            // 获取到我们的通道
                            SocketChannel channel = (SocketChannel) keyChannel;
                            ByteBuffer allocate = ByteBuffer.allocate(1024);
                            // 读取数据
                            int read = channel.read(allocate);
                            if (read > 0) {
                                // 输出我们的消息
                                System.out.println(new String(allocate.array(), 0, read));
                            }
                        }// end if
                    }
                    iterator.remove();
                }
            } else {
//                System.out.println("没有可用的通道");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        NIOClientService nioClientService = new NIOClientService(9999);
        new Thread(()->{
            while (true) {
                nioClientService.readInfo();
                try {
                    Thread.currentThread().sleep(1000);
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String command = scanner.nextLine();
            nioClientService.sendInfo(command);
        }
    }
}
